package com.shinhye.demoproject;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.shinhye.demoproject.adapter.DrawerAdapter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {

    private SQLiteDatabase db;
    private DatabaseHelper databaseHelper;
    private DrawerLayout mDrawerLayout;
    private ListView listView;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            Window win = getWindow();
//            WindowManager.LayoutParams winParams = win.getAttributes();
//            int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
//            winParams.flags |= bits;
//            win.setAttributes(winParams);
//        }

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        listView = (ListView) findViewById(R.id.lv);
        initListView();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            private Snackbar snackbar;
            @Override
            public void onClick(View view) {
                snackbar = Snackbar.make(view, "Open KeyBoard Activity", Snackbar.LENGTH_LONG)
                    .setAction("Sure", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivity(new Intent(MainActivity.this, KeyBoardActivity.class));
                        }
                    });
                snackbar.show();
            }
        });

        initData();

    }

    private void initListView() {
        listView.setAdapter(new DrawerAdapter());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            startActivity(new Intent(this, SettingsActivity.class));
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void onClickButton(View v) {
        switch (v.getId()) {
            case R.id.button1:
                startActivity(new Intent(this, LoginActivity.class));
                break;
            case R.id.button2:
                startActivity(new Intent(this, ItemListActivity.class));
                break;
            case R.id.button3:
                startActivity(new Intent(this, NavigationActivity.class));
                break;
            case R.id.button4:
                startActivity(new Intent(this, ScrollingActivity.class));
                break;
            case R.id.button5:
                startActivity(new Intent(this, AlipayActivity.class));
                break;
            case R.id.button6:
                startActivity(new Intent(this, MaterialActivity.class));
                break;
            case R.id.button7:
                startActivity(new Intent(this, DragDemoActivity.class));
                break;
            case R.id.button8:
                startActivity(new Intent(this, ListAnimationActivity.class));
//                Cursor cursor = db.rawQuery("select * from tb_region where level = 1", null);
//                cursor.moveToLast();
//                StringBuffer sb = new StringBuffer();
//                for(int i = 0;i < cursor.getColumnCount(); i++) {
//                    sb.append(cursor.getString(i));
//                }
//                Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
//                cursor.close();
                break;
            case R.id.button9:
                startActivity(new Intent(this, LogActivity.class));
                break;
            case R.id.button10:
                startActivity(new Intent(this, ViewDragActivity.class));
                break;
            case R.id.button11:
                startActivity(new Intent(this, BehaviorActivity.class));
                break;
            case R.id.button12:
                startActivity(new Intent(this, DragPageActivity.class));
                break;
            case R.id.button13:
                startActivity(new Intent(this, ToolBarActivity.class));
                break;
        }
    }

    private void initData() {
        databaseHelper = new DatabaseHelper(this);
        db = databaseHelper.getWritableDatabase();

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(Gravity.LEFT);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        databaseHelper.close();
    }

    private class DatabaseHelper extends SQLiteOpenHelper
    {

        DatabaseHelper(Context context)
        {
            super(context, "address.db", null, 1);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            BufferedReader in = null;
            try
            {
                InputStream is = MainActivity.this.getAssets().open("tb_region.sql");
                in = new BufferedReader(new InputStreamReader(is));

                String line;
                String buffer = "";
                while ((line = in.readLine()) != null) {
                    buffer += line;
                    if (line.trim().endsWith(";")) {
                        db.execSQL(buffer.replace(";", ""));
                        buffer = "";
                    }
                }


//                ByteArrayOutputStream baos = new ByteArrayOutputStream(410000);
//                int i;
//                while ((i = is.read()) != -1) {
//                    baos.write(i);
//                }
//                String str = baos.toString();
//
//                FileOutputStream os = new FileOutputStream(getCacheDir() + "/sql.txt");
//                os.write(str.getBytes());
//                os.close();
//
//                db.execSQL(str);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            finally {
                try {
                    if (in != null)
                        in.close();
                } catch (IOException e) {
                    Log.e("db-error", e.toString());
                }
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }
}
